#include "windows.h"
#include "common.h"
#include <stdlib.h>


#pragma comment(lib, "DDraw.lib");

bool GetVideoInfo(VIDEO_INFO* info)
{
    IDirectDraw7*		lpIDirectDraw7;
	LPDIRECTDRAW7		m_pDirectDraw;
	LPDIRECTDRAWCLIPPER m_pClipper;
	//LPDIRECTDRAWSURFACE7 m_pSurfacePrimary;
	LPDIRECTDRAWSURFACE7 lpIDirectDrawSurface7;

	DDSURFACEDESC2 SurfaceDesc = { 0 };

	HRESULT hr;
	IDirectDraw * lpIDirectDraw = NULL;
	IDirectDrawSurface * pDDSPrimary = NULL;
	DDSURFACEDESC ddsd;

	char	BufferMsg[0x100] = {0};
	hr = DirectDrawCreate(NULL, &lpIDirectDraw, NULL);
	if (DD_OK != hr)
	{
		sprintf(BufferMsg, "DirectDrawCreate error ->: %d", GetLastError());
		OutputDebugStringA(BufferMsg);
		return 0;
	}
	lpIDirectDraw->QueryInterface(IID_IDirectDraw7, (void**)&lpIDirectDraw7);

	lpIDirectDraw7->SetCooperativeLevel(0, 8);

	SurfaceDesc.dwSize = 0x7c;
	SurfaceDesc.dwFlags = 1;
	SurfaceDesc.ddsCaps.dwCaps = 0x200;

	lpIDirectDraw7->CreateSurface(&SurfaceDesc, &lpIDirectDrawSurface7, NULL);

	lpIDirectDrawSurface7->Lock(NULL, &SurfaceDesc, 0, NULL);

	DWORD size = SurfaceDesc.dwSize;
	DWORD Height = SurfaceDesc.dwHeight;
	DWORD width = SurfaceDesc.dwWidth;
	DWORD lPitch = SurfaceDesc.lPitch;

	info->dwHeight = SurfaceDesc.dwHeight;
	info->dwWidth = SurfaceDesc.dwWidth;
	info->lPitch = SurfaceDesc.lPitch;
	info->pVideoBuffer = (DWORD)SurfaceDesc.lpSurface;

	lpIDirectDrawSurface7->Release();
	return 1;
}

bool AllocExeLogMem()
{
	VirtualFree(LpExeCodeLogMm, NULL, MEM_RELEASE);

	LpExeCodeLogMm = VirtualAlloc(NULL, ExeCodeLogSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

	if (!LpExeCodeLogMm)
	{
     	return false;
	}

	memset(LpExeCodeLogMm, 0x1, ExeCodeLogSize);
	return true;
}

bool AllocMapCodeMem()
{
	VirtualFree(LpCodeMapMem, NULL, MEM_RELEASE);

	LpCodeMapMem = VirtualAlloc(NULL, CodeMapSize, MEM_COMMIT | MEM_RESERVE,
								PAGE_EXECUTE_READWRITE);
	if (!LpCodeMapMem)
	{
     	return false;
	}

	memset(LpCodeMapMem, 0x0, CodeMapSize);
	return true;
}

bool AllocMmBlock()
{
	VirtualFree(LpGMmBlock, NULL, MEM_RELEASE);

	LpGMmBlock = VirtualAlloc(NULL, GMmBlock, MEM_COMMIT | MEM_RESERVE,
								PAGE_EXECUTE_READWRITE);
	if (!LpGMmBlock)
	{
     	return false;
	}

	memset(LpGMmBlock, 0x0, GMmBlock);
	return true;
}

bool GetAnalysePage(BYTE* Buffer)
{
	//
	DWORD dwResult ;
	DWORD buffersize = 1024 * 16;
	HANDLE  hFile = CreateFileA("C:\\PageAnalyse.bin",               // file to open
								GENERIC_READ|GENERIC_WRITE,          // open for reading
								FILE_SHARE_READ,       // share for reading
								NULL,                  // default security
								OPEN_EXISTING,         // existing file on
								FILE_ATTRIBUTE_NORMAL, // normal file
								NULL);
	if (!hFile)
	{
    	return false;
	}
	if (!ReadFile(hFile, Buffer, buffersize, &dwResult, NULL))
	{
		::MessageBoxA(NULL, "ReadFie PageAnalyse.bin fail", "xxxx", MB_OK);
		return false;
	}

	CloseHandle(hFile);

    return true;
}


